//
//  EGORefreshTableBottomView.m
//  Simple Table
//
//  Created by  phoenixnet on 11-1-6.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EGORefreshTableBottomView.h"
#import "CLGloble.h"


#define TEXT_COLOR	 [UIColor colorWithRed:134.0/255.0 green:134.0/255.0 blue:134.0/255.0 alpha:1.0]
#define FLIP_ANIMATION_DURATION 0.18f


@interface EGORefreshTableBottomView (Private)
- (void)setState:(EGOPullRefreshState)aState;
@end

@implementation EGORefreshTableBottomView

@synthesize delegate=_delegate;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
		
		self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 35.0f, self.frame.size.width, 20.0f)];
		label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		label.font = [UIFont systemFontOfSize:12.0f];
		label.textColor = TEXT_COLOR;
		//label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
		label.shadowOffset = CGSizeMake(0.0f, 1.0f);
		label.backgroundColor = [UIColor clearColor];
		label.textAlignment = UITextAlignmentCenter;
		[self addSubview:label];
		_lastUpdatedLabel=label;
		
		label = [[UILabel alloc] initWithFrame:CGRectMake(110.0f+24.0f+7.0f, 15.0f, self.frame.size.width, 20.0f)];
		label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		label.font = [UIFont boldSystemFontOfSize:13.0f];
		label.textColor = [UIColor colorWithRed:0x86/255.0 green:0x86/255.0 blue:0x86/255.0 alpha:1];
		//label.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
		label.shadowOffset = CGSizeMake(0.0f, 1.0f);
		label.backgroundColor = [UIColor clearColor];
		label.textAlignment = UITextAlignmentLeft;
		[self addSubview:label];
		_statusLabel=label;
		
		CALayer *layer = [CALayer layer];
		layer.frame = CGRectMake(110.0f, 10.0f, 24.0f, 24.0f);
		layer.contentsGravity = kCAGravityResizeAspect;
		layer.contents = (id)[UIImage imageNamed:@"list_EGO_UpArrow.png"].CGImage;
        
		
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
		if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
			layer.contentsScale = [[UIScreen mainScreen] scale];
		}
#endif
		
		[[self layer] addSublayer:layer];
		_arrowImage=layer;
		
		UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
		view.frame = CGRectMake(110.0f, 10.0f, 24.0f, 24.0f);
		[self addSubview:view];
		_activityView = view;
		
		[self setState:EGOOPullRefreshNormal];
		
    }
	
    return self;
	
}


#pragma mark -
#pragma mark Setters

//- (void)refreshPageInfo {
//	
//	if ([_delegate respondsToSelector:@selector(egoRefreshTableBottomCurrentPage:)]
//		&& [_delegate respondsToSelector:@selector(egoRefreshTableBottomTotalPage:)]) {
//		NSInteger curPage = [_delegate egoRefreshTableBottomCurrentPage:self];
//		NSInteger totalPage = [_delegate egoRefreshTableBottomTotalPage:self];
//		
//		if (curPage == totalPage) {
//			_lastUpdatedLabel.text = [NSString stringWithFormat:@"当前页数：%d/%d", curPage, totalPage];
//		}else {
//			_lastUpdatedLabel.text = [NSString stringWithFormat:@"当前页数：%d/%d", curPage, totalPage];
//		}		
//	} else {
//		
//		_lastUpdatedLabel.text = nil;
//	}
//}

- (void)setState:(EGOPullRefreshState)aState{
	
	switch (aState) {
		case EGOOPullRefreshPulling:
         _statusLabel.text = [NSString stringWithFormat:@"%@",NSLocalizedString(@"松开加载更多", @"Release to refresh status")];
			[CATransaction begin];
			[CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
			_arrowImage.transform = CATransform3DMakeRotation((M_PI / 180.0) * 180.0f, 0.0f, 0.0f, 1.0f);
			[CATransaction commit];
			
			break;
		case EGOOPullRefreshNormal:
			
			if (_state == EGOOPullRefreshPulling) {
				[CATransaction begin];
				[CATransaction setAnimationDuration:FLIP_ANIMATION_DURATION];
				_arrowImage.transform = CATransform3DIdentity;
				[CATransaction commit];
			}
			
			_statusLabel.text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"上拉加载更多", @"Pull down to refresh status")];
			[_activityView stopAnimating];
			[CATransaction begin];
			[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; 
			_arrowImage.hidden = NO;
			_arrowImage.transform = CATransform3DIdentity;
			[CATransaction commit];
			
			//[self refreshPageInfo];
			
			break;
		case EGOOPullRefreshLoading:
			
			_statusLabel.text =[NSString 
                          stringWithFormat:@"%@",
                         NSLocalizedString(@"正在加载", @"Loading Status")];
      
			[_activityView startAnimating];
			[CATransaction begin];
			[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; 
			_arrowImage.hidden = YES;
			[CATransaction commit];
			//[NSThread sleepForTimeInterval:2];
			break;
		default:
			break;
	}
	
	_state = aState;
}

- (void)egoChangeBottomItemFrame:(UIScrollView *)scrollView{
	if (self.frame.origin.y != scrollView.contentSize.height) {
        CGRect rect = self.frame;
        rect.origin = CGPointMake(0, scrollView.contentSize.height);
    if (rect.origin.y < ((GlobleHeight-32))) {
            rect.origin = CGPointMake(0, ((GlobleHeight-32)));
        }
        self.frame = rect;
	 }	
}

#pragma mark -
#pragma mark ScrollView Methods

- (void)egoRefreshScrollViewDidScroll:(UIScrollView *)scrollView {	
	
	float contentSizeHeight = scrollView.contentSize.height;
	float scrollViewHeight = scrollView.frame.size.height;
    float contentOffSetY = scrollView.contentOffset.y;
    
	if (contentSizeHeight == 0 || contentSizeHeight == 61) {
		return ;
	}

	if (_state == EGOOPullRefreshLoading) {
		//scrollView.contentOffset.y * -1
/*		CGFloat offset = MAX(scrollView.contentOffset.y+listHeight-totalHeight, 0);
		offset = MIN(offset, 60);
		scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, offset, 0.0f);*/
		
	} else if (scrollView.isDragging) {
		BOOL _loading = NO;
		if ([(NSObject *)self.delegate respondsToSelector:@selector(egoRefreshTableBottomDataSourceIsLoading:)]) {
			_loading = [_delegate egoRefreshTableBottomDataSourceIsLoading:self];
		}
		
//		float offset = contentSizeHeight-contentOffSetY-scrollViewHeight;
//        NSLog(@"offset=%f",offset);
//		if (_state == EGOOPullRefreshPulling && offset > -65.0f && offset < 0.0f && !_loading) {
//            NSLog(@"normal");
//			[self setState:EGOOPullRefreshNormal];
//		}
//        else if (_state == EGOOPullRefreshNormal && offset < -65.0f && !_loading) {
//            NSLog(@"pulling");
//			[self setState:EGOOPullRefreshPulling];
//		}

		float offset = 0;
        if(contentSizeHeight > scrollViewHeight)
            
            offset = contentOffSetY + scrollViewHeight - contentSizeHeight;
        else
            offset = contentOffSetY;
        if(_state == EGOOPullRefreshPulling && offset < 65.0 && !_loading){
            [self setState:EGOOPullRefreshNormal];
        }
        else if(_state == EGOOPullRefreshNormal && offset > 65.0f && !_loading){
            [self setState:EGOOPullRefreshPulling];
        }
        
		if (scrollView.contentInset.top != 0) {
			scrollView.contentInset = UIEdgeInsetsZero;
		}
		
	}
}

- (void)egoRefreshScrollViewDidEndDragging:(UIScrollView *)scrollView {
	
	NSInteger totalHeight = scrollView.contentSize.height;
	NSInteger listHeight = scrollView.frame.size.height;
   
	if (totalHeight == 0 || totalHeight ==61) {
		return ;
	}
	
	//NSInteger curPage = [_delegate egoRefreshTableBottomCurrentPage:self];
	//NSInteger totalPage = [_delegate egoRefreshTableBottomTotalPage:self];
	
//	if (curPage == totalPage)
//	{
//		return ;
//	}
	
	BOOL _loading = NO;
	if ([(NSObject *)self.delegate respondsToSelector:@selector(egoRefreshTableBottomDataSourceIsLoading:)]) {
		_loading = [self.delegate egoRefreshTableBottomDataSourceIsLoading:self];
	}
	
	
	float offset = totalHeight - scrollView.contentOffset.y - listHeight;
 
	if (offset <= - 65.0f && !_loading) {
		
		if ([(NSObject *)self.delegate respondsToSelector:@selector(egoRefreshTableBottomDidTriggerRefresh:)]) {
			[self.delegate egoRefreshTableBottomDidTriggerRefresh:self];
		}
		
		[self setState:EGOOPullRefreshLoading];
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.2];
		CGFloat top = scrollView.contentInset.top;
		scrollView.contentInset = UIEdgeInsetsMake(top, 0.0f, 60.0f, 0.0f);
		[UIView commitAnimations];
	}
}

- (void)egoRefreshScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {	
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:.3];
	CGFloat top = scrollView.contentInset.top ;
	[scrollView setContentInset:UIEdgeInsetsMake(top, 0.0f, 0.0f, 0.0f)];
	[UIView commitAnimations];
	
	[self setState:EGOOPullRefreshNormal];
	
}


#pragma mark -
#pragma mark Dealloc

- (void)dealloc {
	
	_delegate=nil;
	_activityView = nil;
	_statusLabel = nil;
	_arrowImage = nil;
	_lastUpdatedLabel = nil;
}


@end

